package com.yesep.learn.jwt.handler;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import com.yesep.learn.jwt.common.R;
import com.yesep.learn.jwt.util.ServletUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 认证失败 需要做的业务操作 当检测到用户访问系统资源认证失败时则会进入到此类并执行相关业务
 */
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
	@Override
	public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
		response.setStatus(HttpServletResponse.SC_FORBIDDEN);
		ServletUtils.writeValueAsString(R.me(HttpServletResponse.SC_FORBIDDEN, authException.getMessage()), response);
	}
}
